package com.dingding.special.order.cancel.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.base.daox.ISQLBaseDao;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.special.Userspecialorder;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.special.base.enums.OrderEnums;
import com.dingding.special.order.cancel.dao.ICancelSpecialOrderDao;
import com.dingding.special.order.cancel.dto.CancelSpecialOrderNoticeDTO;
import com.dingding.special.order.cancel.dto.ReqCancelSpecialOrderDTO;
import com.dingding.special.order.cancel.dto.ResCancelSpecialOrderDTO;
import com.dingding.special.order.cancel.sql.CancelSpecialOrderSQL;
import com.dingding.special.order.confirm.code.CancelSpecialOrderCodeMsg;

/**
 * 
 * @ClassName: CancelSpecialOrderDaoImpl 
 * @Description: 取消专车订单DAO实现类
 * @author YHQ
 * @date 2016年8月21日 下午7:40:57
 * @since V2.4.0
 * 
 */
@Component("cancelSpecialOrderDao")
public class CancelSpecialOrderDaoImpl implements ICancelSpecialOrderDao{

	@Resource(name="criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;
	
	@Resource(name="SQLBaseDao")
	private ISQLBaseDao sqlBaseDao;
	
	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * 
	 * @Title: getUserSpecialOrdertInfo 
	 * @Description: 根据订单Id获取专车订单信息
	 * @param oid 订单Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return UserspecialorderT    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public UserspecialorderT getUserSpecialOrdertInfo(String oid) {
		
		Criterion criterion = Restrictions.eq("specialOrderId", oid);
		
		try {
			return criteriaBaseDao.queryCriteriaListUnique(UserspecialorderT.class,criterion);
		} catch (Exception e) {
			log.error("根据用户Id获取专车未完成订单时出现异常   类："+this.getClass()+" getUserSpecialOrdertInfo()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getNoticeInfo 
	 * @Description: 获取取消订单通知信息
	 * @param oid 订单Id
	 * @param cancelType 取消类型   1:乘客取消 2:车主取消 
	 * @author YHQ 
	 * @return CancelSpecialOrderSQL    返回类型
	 * 
	 */
	@Override
	public CancelSpecialOrderNoticeDTO getNoticeInfo(String oid, int cancelType) {
		
		String querySql = null;
		
		if(cancelType == 1){
			querySql = CancelSpecialOrderSQL.cancelSpecialOrderNoticeInfo(cancelType);
		}else{
			querySql = CancelSpecialOrderSQL.CANCEL_SPECIAL_FROM_DRIVER;
		}
		
		try {
			// 根据订单Id和取消人，获取取消推送内容
			List<CancelSpecialOrderNoticeDTO> cancelOrderNotices = sqlBaseDao.listSqlToCustomBean(CancelSpecialOrderNoticeDTO.class, querySql, oid);
			
			if(cancelOrderNotices != null && cancelOrderNotices.size() > 0){
				
				CancelSpecialOrderNoticeDTO dto = cancelOrderNotices.get(0);
				
				if(dto.getLoginEquipment() == 1){
					dto.setClientType("iOS");
				}else{
					dto.setClientType("Android");
				}
				
				return cancelOrderNotices.get(0);
			}
		} catch (Exception e) {
			log.error("获取取消订单通知信息时出现异常   类："+this.getClass()+" getNoticeInfo()", e);
		}
		
		return null;
	}
	
	
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public UserspecialorderT operateOldOrderT(ResponseEntity<ResCancelSpecialOrderDTO> response,
			ReqCancelSpecialOrderDTO reqCancelSpecialOrderDTO) {
		// 根据用户Id获取专车订单信息
		UserspecialorderT userspecialorderT = getUserSpecialOrdertInfo(reqCancelSpecialOrderDTO.getOid());

		// 判断该订单是否存在
		if (userspecialorderT == null) {

			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(CancelSpecialOrderCodeMsg.ORDER_NOT_EXIST);// 订单不存在
			response.setPromptMessage(CancelSpecialOrderCodeMsg.ORDER_NOT_EXIST);// 订单不存在
			return null;
		}

		// 专车订单正式表
		Userspecialorder userspecialorder = new Userspecialorder();
		BeanUtils.copyProperties(userspecialorderT, userspecialorder);

		// 订单Id
		userspecialorder.setSpecialOrderId(ToolUtil.getUid());
		// 取消原因
		userspecialorder.setCancelDesc(reqCancelSpecialOrderDTO.getCancelDesc());
		// 取消类型
		userspecialorder.setCancelType(reqCancelSpecialOrderDTO.getCancelType());
		// 取消时间
		userspecialorder.setCancelTime(new Date());
		// 是否评论
		userspecialorder.setIsComment(1);
		// 订单状态
		userspecialorder.setOrderStatus(6);

		/***********乘客发布订单加入待指派订单*************/
		// 抢单人
		userspecialorderT.setGrabOrderUserId(null);
		// 抢单时间
		userspecialorderT.setAppointTime(null);
		// 待指派
		userspecialorderT.setOrderStatus(OrderEnums.STAY_ASSIGN.getIndex());

		try {

			// 保存待取消订单到正式表
			criteriaBaseDao.save(userspecialorder);

			// 重新发布订单
			criteriaBaseDao.update(userspecialorderT);

		} catch (Exception e) {
			log.error("取消专车订单时出现异常   类：" + this.getClass() + " cancelSpecialOrderOperation()", e);
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(CancelSpecialOrderCodeMsg.CANCEL_ORDER_FAIL);// 取消订单失败
			response.setPromptMessage(CancelSpecialOrderCodeMsg.CANCEL_ORDER_FAIL);// 取消订单失败
			return null;
		}
		return userspecialorderT;
	}

}
